package com.triones.api.controller.api.interlace;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.triones.api.entity.bo.EmailBO;
import com.triones.api.entity.bo.LoginBO;
import com.triones.api.entity.bo.MemberBO;
import com.triones.api.entity.interlace.dto.InterlaceMemberDO;
import com.triones.api.entity.interlace.vo.InterlaceMemberVO;
import com.triones.api.service.interlace.InterlaceLoginService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * api - interlace 注册登录
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/interlace")
public class ApiInterlaceLoginController extends BaseController {

    private final InterlaceLoginService interlaceLoginService;

    /**
     * 注册
     * @return
     */
    @SaIgnore
    @RepeatSubmit()
    @PostMapping("/register")
    public R<Void> register(@RequestBody MemberBO bo) {
        interlaceLoginService.InterlaceRegister(bo);
        return R.ok("注册成功");
    }

    /**
     * 登录
     * @return
     */
    @SaIgnore
    @RepeatSubmit()
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginBO bo) {
        return R.ok("登录成功！",interlaceLoginService.interlaceLogin(bo));
    }

    /**
     * 校验邮箱是否已被注册和是否合规
     * @return
     */
    @SaIgnore
    @GetMapping("/checkEmail")
    public R<Boolean> checkEmailUnique(@NotBlank( message = "邮箱不能为空") String email) {
        return R.ok(interlaceLoginService.checkEmailUnique(email));
    }

    /**
     * 获取用户基本信息
     * @return
     */
    @GetMapping("/memberInfo")
    public R<InterlaceMemberVO> getMemberInfo() {
        return R.ok(interlaceLoginService.getMemberInfo());
    }

    /**
     * 编辑用户信息
     * @return
     */
    @PutMapping("/editMember")
    public R<Void> editMember(@RequestBody InterlaceMemberDO dto) {
        return toAjax(interlaceLoginService.editMember(dto));
    }

    /**
     * 设备更新
     */
    @PostMapping("/device")
    public R<Void> device(@NotBlank(message = "设备id不能为空") String deviceId) {
        return toAjax(interlaceLoginService.device(deviceId));
    }

    /**
     * 语言更新
     */
    @PostMapping("/language")
    public R<Void> language(@NotBlank(message = "语言不能为空") String language) {
        return toAjax(interlaceLoginService.language(language));
    }

    /**
     * 邮箱验证码校验
     * @return
     */
    @SaIgnore
    @RepeatSubmit()
    @PostMapping("/emailCheck")
    public R<Boolean> emailCheck(@RequestBody EmailBO bo) {
        return R.ok(interlaceLoginService.emailCheck(bo));
    }
}
